package com.yuyou.tas.lesson.service.impl;


import com.yuyou.tas.lesson.mapper.*;
import com.yuyou.tas.lesson.service.IBaseService;
import com.yuyou.tas.service.api.lesson.entity.BaseLevel;
import com.yuyou.tas.service.api.lesson.entity.BaseTeach;
import com.yuyou.tas.util.enums.lesson.LessonBaseEnums;
import com.yuyou.tas.util.tool.DefindBeanUtils;
import com.yuyou.tas.util.tool.Sequence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaseServiceImpl implements IBaseService {

    @Autowired
    private BaseSectionMapper sectionMapper;

    @Autowired
    private BaseLevelMapper levelMapper;

    @Autowired
    private BaseSectionSubjectRelMapper ssrMapper;

    @Autowired
    private BaseTeachMapper teachMapper;

    @Autowired
    private BaseTrimesterMapper trimesterMapper;


    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public boolean copyBaseData(Long userID, Long userOrganId, Long organId) {
        if(organId == null){return false;}
        Date now = new Date();
        Map<String , Object> condition = new HashMap<>();
        condition.put("organ_id",userOrganId);
        condition.put("isUse", LessonBaseEnums.isEnableed.enabl.getValue());
        condition.put("isStan", 1);

        Map<String , String> sectionMap = new HashMap<>();//新旧年级ID对应

        //复制年级
        /*List<BaseSection> sections = sectionMapper.findSections(condition);
        for (BaseSection bs:sections) {
            BaseSection b = new BaseSection();
            DefindBeanUtils.copyProperties(bs , b);
            Long newSectionId = Sequence.getSequence().nextId();
            sectionMap.put(bs.getId().toString(),newSectionId.toString());

            b.setId(newSectionId);
            b.setpId(bs.getId());
            b.setCreateUser(null);
            b.setIsStan(1);
            b.setCreateTime(now);
            b.setAuthOrganizationId(organId);
            b.setValue(b.getId());

            sectionMapper.insert(b);
        }*/

        //复制层级
        List<BaseLevel> levels = levelMapper.findLevels(condition);
        for (BaseLevel old: levels
             ) {
            BaseLevel bl = new BaseLevel();
            DefindBeanUtils.copyProperties(old , bl);

            bl.setId(Sequence.getSequence().nextId());
            bl.setpId(old.getId());
            bl.setCreateUser(null);
            bl.setCreateTime(now);
            bl.setIsStan(1);
            bl.setAuthOrganizationId(organId);
            bl.setValue(bl.getId());

            levelMapper.insert(bl);
        }

        //复制科目年级关系
        /*List<BaseSectionSubjectRel> ssrList = ssrMapper.findSSRs(condition);
        for (BaseSectionSubjectRel old: ssrList
             ) {
            BaseSectionSubjectRel ssr = new BaseSectionSubjectRel();
            ssr.setId(Sequence.getSequence().nextId());
            ssr.setCreateTime(now);
            ssr.setAuthOrganizationId(organId);
            ssr.setIsStan(1);
            ssr.setDataSubject(old.getDataSubject());
            //替换原来的年级ID，换为新增的ID
            String[] sectionIds = old.getBaseSectionId().split(",");
            for (int i=0 ; i<sectionIds.length ; i++){
                if(sectionMap.containsKey(sectionIds[i])){
                    String id = sectionIds[i];
                    sectionIds[i] = sectionMap.get(id);
                }
            }
            ssr.setBaseSectionId(String.join(",",sectionIds));
            ssrMapper.insert(ssr);
        }*/

        //复制教材版本信息
        List<BaseTeach> teachList = teachMapper.findTeachs(condition);
        for (BaseTeach old:teachList
             ) {
            BaseTeach bt = new BaseTeach();
            DefindBeanUtils.copyProperties(old , bt);
            bt.setId(Sequence.getSequence().nextId());
            bt.setpId(old.getId());
            bt.setAuthOrganizationId(organId);
            bt.setCreateTime(now);
            bt.setCreateUser(null);
            bt.setIsStan(1);
            bt.setValue(bt.getId());

            //替换原来的年级ID，换为新增的ID
            String[] sectionIds = old.getDataSection().split(",");
            for (int i=0 ; i<sectionIds.length ; i++){
                if(sectionMap.containsKey(sectionIds[i])){
                    String id = sectionIds[i];
                    sectionIds[i] = sectionMap.get(id);
                }
            }
            bt.setDataSection(String.join(",",sectionIds));
            teachMapper.insert(bt);
        }


        //复制学期信息
        /*List<BaseTrimester> trimesterList = trimesterMapper.findTerms(condition);
        for (BaseTrimester old : trimesterList
             ) {
            BaseTrimester bt = new BaseTrimester();
            bt.setpId(old.getId());
            bt.setId(Sequence.getSequence().nextId());
            bt.setName(old.getName());
            bt.setRemark(old.getRemark());
            bt.setAuthOrganizationId(organId);
            bt.setIsStan(1);
            bt.setIsUse(LessonBaseEnums.isEnableed.enabl.getValue());
            bt.setCreateTime(now);
            bt.setValue(bt.getId());
            bt.setCode(old.getCode());
            trimesterMapper.insert(bt);
        }*/


        return true;
    }
}